﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using Client.Service;
using GenericModels;
using System.Text.RegularExpressions;


namespace Client
{
    public partial class UserCtlCustomer : UserControl
    {
        public delegate void UserControlClickHandler(object sender, EventArgs e);
        public event UserControlClickHandler InnerButtonClickProceed;
        public event UserControlClickHandler InnerButtonClickSearch;
        public event UserControlClickHandler InnerButtonClickCustomer;
        public Employee employee { get; set; }
        public Customer customer { get; set; }
       

        private IPerson personService;
        private ICities cityService;

        public UserCtlCustomer(Employee employee)
        {
            this.employee = employee;
            InitializeComponent();
            personService = new PersonClient();
            cityService = new CitiesClient();
            LoadEmployeePic();

        }

        //Tjener kun lige til at springe skærmen over, indtil det bliver ordentligt implementeret
        private void buttonCheat_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClickProceed != null)
            {
                this.InnerButtonClickProceed(sender, e);
            }
        }

        public static bool VerifyCpr(string toVerify)
        {
            string pattern = @"([0-9]+)";

            return Regex.IsMatch(toVerify, pattern);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClickSearch != null)
            {
                string phoneNo = textBox1.Text;

                if (phoneNo != null && phoneNo.Length == 8)
                {
                    if (VerifyCpr(phoneNo) == true)
                    {
                        Customer cus = personService.GetCustomerByPhoneNo(phoneNo);
                        if (cus != null)
                        {
                            customer = RetreiveCityOnCustomer(cus);

                            textBoxName.Text = customer.FName;
                            textBoxAddress.Text = customer.Address;
                            textBoxPostal.Text = customer.PostalCode.ToString();
                            textBoxCity.Text = customer.City;
                            textBoxEmail.Text = customer.Email;
                            this.InnerButtonClickSearch(sender, e);
                        }
                        else
                        {
                            MessageBox.Show("ingen kunde fundet");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Tlf nr skal bestå af tal" + phoneNo.ToString());
                    }
                }

                else
                {
                    MessageBox.Show("indtastning skal være 8 karakter lang og bestå af tal!");
                }
            }
        }

        private Customer RetreiveCityOnCustomer(Customer cus)
        {
            City city = cityService.RetrieveCity(cus.PostalCode);

            cus.City = city.CityName;

            return cus;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClickCustomer != null)
            {
                this.InnerButtonClickCustomer(sender, e);
            }
        }

        public Customer makeCheatCustomer()
        {
            customer = personService.GetCustomerByPhoneNo("11223344");
            customer.City = RetreiveCityOnCustomer(customer).City;
            return customer;
        }

        private void LoadEmployeePic()
        {
            pictureBox1.ImageLocation = "http://www.mynetimages.com/4f992650cb.jpg";
            lblEmpName.Text = employee.FName;
            lblEmpTitle.Text = employee.Title;
        }

    }
}
